﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;  
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Sale_DAL
{
    public partial class Config : Form
    {
        public Config()
        {
            InitializeComponent();
        }

        private void Config_Load(object sender, EventArgs e)
        {
            //打开配置文件，
            FileStream fs= File.Open("./Config", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            System.IO.StreamReader reader = new StreamReader(fs);
            string Config = reader.ReadToEnd();
            string[] strs = Config.Split(',');
            if (strs.Length > 0)
            {
                te_DataSource.Text = strs[0];
            }
            if (strs.Length > 1)
            {
                te_DataBase.Text = strs[1];
            }
            if (strs.Length > 2)
            {
                te_User.Text = strs[2];
            }
            if (strs.Length > 3)
            {
                te_PWD.Text = strs[3];
            }
            reader.Close();
            fs.Close();

        }

        /// <summary>
        /// 保存配置信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_Save_Click(object sender, EventArgs e)
        {

            System.IO.StreamWriter writer = new StreamWriter("./Config");
            writer.Write(te_DataSource.Text.Replace(",", "") + "," + te_DataBase.Text.Replace(",", "") + "," + te_User.Text.Replace(",", "") + "," + te_PWD.Text.Replace(",", "") + ",");
            writer.Close();
 
            if (MessageBox.Show( "保存完成！是否关闭窗体？","提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void btn_Connect_Click(object sender, EventArgs e)
        {
            
            btn_Connect.Enabled = false;
            btn_Connect.Text = "连接中..";
            using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection())
            {
                try
                {
                    connection.ConnectionString = "server=" + te_DataSource.Text + ";database=" + te_DataBase.Text + ";pwd=" + te_PWD.Text + ";uid=" + te_User.Text;
                    connection.Open();
                    connection.Close();
                    MessageBox.Show( "连接成功！","提示！");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            btn_Connect.Text = "测试连接";
            btn_Connect.Enabled =true;
        }

        private void bt_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
